/*************************************************************************
 *
 *  Copyright (C) 2013 Florian Dang <florian.coin@gmail.com>
 *
 *  This file is part of NatusVerse.
 *
 *  NatusVerse is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  NatusVerse is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NatusVerse.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/


#include "raytracer/objects/camera.h"

#include <stdio.h>
#include <cmath>

namespace raytracer
{

Vector Camera::ViewDir() const
{
	Vector N = look_at() - eye();
	N.Normalize();

	return N;
}

Vector Camera::XAxisDir() const
{
	Vector U;

	U = ViewDir() % view_up(); // crossprod
	U.Normalize();

	return U;
}

Vector Camera::UpAxisDir() const
{
	Vector V;

	V = XAxisDir() % ViewDir(); // crossprod
	V.Normalize();

	return V;
}

Vector Camera::Xinc() const
{
	Vector XInc;

	XInc = (2 * tan(fov()/2.0) / w()) * XAxisDir();

	return XInc;
}

Vector Camera::Yinc() const
{
	Vector YInc;

	YInc = (2 * tan(fov()/2.0) / w()) * UpAxisDir();

	return YInc;
}

void Camera::Debug() const
{
	Camera::Print();
	printf("\nN");
	ViewDir().Print();
	printf(" U");
	XAxisDir().Print();
	printf(" V");
	UpAxisDir().Print();
	printf("\nXinc");
	Xinc().Print();
	printf(" Yinc");
	Yinc().Print();
}

void Camera::Print() const
{
	printf("LF");
	eye().Print();
	printf(" LA");
	look_at().Print();
	printf(" VUP");
	view_up().Print();
	printf(" fov{%.2f} {%dx%d}", fov(), w(), h());
}
}
